//
//  UIApplication+STSystemTool.m
//  catVoice
//
//  Created by imac on 2019/6/25.
//  Copyright © 2019年 imac. All rights reserved.
//

#import "UIApplication+STTools_SystemTool.h"
#import <sys/utsname.h>
@implementation UIApplication (STTools_SystemTool)
+ (NSString *)stoneobs_fun_st_applictionVersin{
    NSString * stoneobs_temp_version =  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];;
    return stoneobs_temp_version;
}
+  (NSString *)stoneobs_fun_st_applictionBuildNum{
    NSString * stoneobs_temp_build =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    return stoneobs_temp_build;
}
+ (NSString *)stoneobs_fun_st_applictionBundleIdentifier{
    NSString * stoneobs_temp_bundleID =  [[NSBundle mainBundle] bundleIdentifier];
    return stoneobs_temp_bundleID;
}
+ (NSString *)stoneobs_fun_st_applictionDisplayName{
    NSString * stoneobs_temp_name = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    return stoneobs_temp_name;
}
+ (NSDictionary *)stoneobs_fun_st_applictionInfoPlist{
    NSDictionary * stoneobs_temp_dic = [[NSBundle mainBundle] infoDictionary];
    return stoneobs_temp_dic;
}
+ (NSString *)stoneobs_fun_st_uuid{
    NSString * stoneobs_temp_uuid =  [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    return stoneobs_temp_uuid;
}
+ (void)stoneobs_fun_st_saveUUIDToKeyChain:(NSString *)uuid{
    NSString * stoneobs_temp_bundleID = [UIApplication stoneobs_fun_st_applictionBundleIdentifier];
    NSString * stoneobs_temp_defultPwd = uuid;
    if (uuid.length) {
        NSMutableDictionary *queryDic = [NSMutableDictionary dictionary];
        //账号密码 账号和serviece 都是bundleid  密码就是uuid
        [queryDic setObject:stoneobs_temp_bundleID forKey:(__bridge id)kSecAttrService];
        [queryDic setObject:stoneobs_temp_bundleID forKey:(__bridge id)kSecAttrAccount];
        [queryDic setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];//表明存储的是一个密码
        OSStatus status = -1;
        CFTypeRef result = NULL;
        status = SecItemCopyMatching((__bridge CFDictionaryRef)queryDic, &result);
        if (status == errSecItemNotFound) {
            //没有找到则添加
            NSData *passwordData = [stoneobs_temp_defultPwd dataUsingEncoding:NSUTF8StringEncoding];
            //把password 转换为 NSData
            [queryDic setObject:passwordData forKey:(__bridge id)kSecValueData];
            //添加密码
            status = SecItemAdd((__bridge CFDictionaryRef)queryDic, NULL);
            //!!!!!关键的添加API
            
        }else if (status == errSecSuccess){
            //成功找到，说明钥匙已经存在则进行更新
            NSData *passwordData = [stoneobs_temp_defultPwd dataUsingEncoding:NSUTF8StringEncoding];
            //把password 转换为NSData
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:queryDic];
            [dict setObject:passwordData forKey:(__bridge id)kSecValueData];
            //添加密码
            status = SecItemUpdate((__bridge CFDictionaryRef)queryDic, (__bridge CFDictionaryRef)dict);
            //!!!!关键的更新API
            
        }
        
        
    }
}
+ (NSString *)stoneobs_fun_st_readUUIDFromKeyChain{
    NSString * bundleID = [UIApplication stoneobs_fun_st_applictionBundleIdentifier];
    if (bundleID.length) {
        NSMutableDictionary *queryDic = [NSMutableDictionary dictionary];
        //首先添加获取密码所需的搜索键和类属性：
        [queryDic setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
        //表明为一般密码可能是证书或者其他东西
        [queryDic setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
        //返回Data
        [queryDic setObject:bundleID forKey:(__bridge id)kSecAttrService];
        //输入service
        [queryDic setObject:bundleID forKey:(__bridge id)kSecAttrAccount];
        
        OSStatus status = -1;
        CFTypeRef result = NULL;
        status = SecItemCopyMatching((__bridge CFDictionaryRef)queryDic, &result);
        if (status == errSecItemNotFound) {
            return @"";
            
        }else if (status == errSecSuccess){
            //成功找到，说明钥匙已经存在则 取出
            //删除kSecReturnData键; 我们不需要它了：
            [queryDic removeObjectForKey:(__bridge id)kSecReturnData];
            //将密码转换为NSString并将其添加到返回字典：
            NSString *password = [[NSString alloc] initWithBytes:[(__bridge_transfer NSData *)result bytes] length:[(__bridge NSData *)result length] encoding:NSUTF8StringEncoding];
            
            [queryDic setObject:password forKey:(__bridge id)kSecValueData];
            NSLog(@"查询 : %@", queryDic);
            return password;
            
        }
        
        
    }
    return @"";
}
+ (NSString*)st_iphone_type{
    return [self iphoneType];
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString*platform = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    return platform;
}
+ (NSString*)iphoneType{
    
    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString*platform = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    
    if([platform isEqualToString:@"iPhone1,1"])  return@"iPhone 2G";
    
    if([platform isEqualToString:@"iPhone1,2"])  return@"iPhone 3G";
    
    if([platform isEqualToString:@"iPhone2,1"])  return@"iPhone 3GS";
    
    if([platform isEqualToString:@"iPhone3,1"])  return@"iPhone 4";
    
    if([platform isEqualToString:@"iPhone3,2"])  return@"iPhone 4";
    
    if([platform isEqualToString:@"iPhone3,3"])  return@"iPhone 4";
    
    if([platform isEqualToString:@"iPhone4,1"])  return@"iPhone 4S";
    
    if([platform isEqualToString:@"iPhone5,1"])  return@"iPhone 5";
    
    if([platform isEqualToString:@"iPhone5,2"])  return@"iPhone 5";
    
    if([platform isEqualToString:@"iPhone5,3"])  return@"iPhone 5c";
    
    if([platform isEqualToString:@"iPhone5,4"])  return@"iPhone 5c";
    
    if([platform isEqualToString:@"iPhone6,1"])  return@"iPhone 5s";
    
    if([platform isEqualToString:@"iPhone6,2"])  return@"iPhone 5s";
    
    if([platform isEqualToString:@"iPhone7,1"])  return@"iPhone 6 Plus";
    
    if([platform isEqualToString:@"iPhone7,2"])  return@"iPhone 6";
    
    if([platform isEqualToString:@"iPhone8,1"])  return@"iPhone 6s";
    
    if([platform isEqualToString:@"iPhone8,2"])  return@"iPhone 6s Plus";
    
    if([platform isEqualToString:@"iPhone8,4"])  return@"iPhone SE";
    
    if([platform isEqualToString:@"iPhone9,1"])  return@"iPhone 7";
    
    if([platform isEqualToString:@"iPhone9,2"])  return@"iPhone 7 Plus";
    
    if([platform isEqualToString:@"iPhone10,1"]) return@"iPhone 8";
    
    if([platform isEqualToString:@"iPhone10,4"]) return@"iPhone 8";
    
    if([platform isEqualToString:@"iPhone10,2"]) return@"iPhone 8 Plus";
    
    if([platform isEqualToString:@"iPhone10,5"]) return@"iPhone 8 Plus";
    
    if([platform isEqualToString:@"iPhone10,3"]) return@"iPhone X";
    
    if([platform isEqualToString:@"iPhone10,6"]) return@"iPhone X";
    
    if([platform isEqualToString:@"iPhone11,8"]) return@"iPhone XR";
    
    if([platform isEqualToString:@"iPhone11,2"]) return@"iPhone XS";
    
    if([platform isEqualToString:@"iPhone11,6"]) return@"iPhone XS Max";
    if([platform isEqualToString:@"iPhone11,4"]) return@"iPhone XS Max";
    
    if([platform isEqualToString:@"iPhone12,1"]) return@"iPhone 11";
    
    if([platform isEqualToString:@"iPhone12,3"]) return@"iPhone 11 Pro";
    if([platform isEqualToString:@"iPhone12,5"]) return@"iPhone 11 Pro Max";
    if([platform isEqualToString:@"iPhone12,8"]) return@"iPhone SE(2020)";
    
    if([platform isEqualToString:@"iPod1,1"])  return@"iPod Touch 1G";
    
    if([platform isEqualToString:@"iPod2,1"])  return@"iPod Touch 2G";
    
    if([platform isEqualToString:@"iPod3,1"])  return@"iPod Touch 3G";
    
    if([platform isEqualToString:@"iPod4,1"])  return@"iPod Touch 4G";
    
    if([platform isEqualToString:@"iPod5,1"])  return@"iPod Touch 5G";
    
    if([platform isEqualToString:@"iPad1,1"])  return@"iPad 1G";
    
    if([platform isEqualToString:@"iPad2,1"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,2"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,3"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,4"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,5"])  return@"iPad Mini 1G";
    
    if([platform isEqualToString:@"iPad2,6"])  return@"iPad Mini 1G";
    
    if([platform isEqualToString:@"iPad2,7"])  return@"iPad Mini 1G";
    
    if([platform isEqualToString:@"iPad3,1"])  return@"iPad 3";
    
    if([platform isEqualToString:@"iPad3,2"])  return@"iPad 3";
    
    if([platform isEqualToString:@"iPad3,3"])  return@"iPad 3";
    
    if([platform isEqualToString:@"iPad3,4"])  return@"iPad 4";
    
    if([platform isEqualToString:@"iPad3,5"])  return@"iPad 4";
    
    if([platform isEqualToString:@"iPad3,6"])  return@"iPad 4";
    
    if([platform isEqualToString:@"iPad4,1"])  return@"iPad Air";
    
    if([platform isEqualToString:@"iPad4,2"])  return@"iPad Air";
    
    if([platform isEqualToString:@"iPad4,3"])  return@"iPad Air";
    
    if([platform isEqualToString:@"iPad4,4"])  return@"iPad Mini 2G";
    
    if([platform isEqualToString:@"iPad4,5"])  return@"iPad Mini 2G";
    
    if([platform isEqualToString:@"iPad4,6"])  return@"iPad Mini 2G";
    
    if([platform isEqualToString:@"iPad4,7"])  return@"iPad Mini 3";
    
    if([platform isEqualToString:@"iPad4,8"])  return@"iPad Mini 3";
    
    if([platform isEqualToString:@"iPad4,9"])  return@"iPad Mini 3";
    
    if([platform isEqualToString:@"iPad5,1"])  return@"iPad Mini 4";
    
    if([platform isEqualToString:@"iPad5,2"])  return@"iPad Mini 4";
    
    if([platform isEqualToString:@"iPad5,3"])  return@"iPad Air 2";
    
    if([platform isEqualToString:@"iPad5,4"])  return@"iPad Air 2";
    
    if([platform isEqualToString:@"iPad6,3"])  return@"iPad Pro 9.7";
    
    if([platform isEqualToString:@"iPad6,4"])  return@"iPad Pro 9.7";
    
    if([platform isEqualToString:@"iPad6,7"])  return@"iPad Pro 12.9";
    
    if([platform isEqualToString:@"iPad6,8"])  return@"iPad Pro 12.9";
    
    if([platform isEqualToString:@"i386"])  return@"iPhone Simulator";
    
    if([platform isEqualToString:@"x86_64"])  return@"iPhone Simulator";
    
    return platform;
    
}
@end
